package com.xixibaby.redis.login;

import com.xixibaby.core.SuperPO;
import com.xixibaby.core.SuperResponse;
import com.xixibaby.core.login.LoginInfoVO;
import com.xixibaby.core.login.LoginRule;
import com.xixibaby.core.Assert;
import com.xixibaby.redis.Dict;

/**
 * @author tooomj
 */
public abstract class UsernameAndPassRule
        <RES extends SuperResponse,PO extends SuperPO>
        implements LoginRule<RES> {

    /**
     *
     * @param username   账号
     * @param password  未加密的password
     * @param paltform  平台标志
     * @return
     */
    @Override
    public RES authorization(String username, String password, int paltform) {
        //数据库是否有数据
        PO po = this.selectPoByAccount(username,paltform);
        String text = Dict.text(LoginInfoVO::getPlatform, paltform);
        Assert.succ(po).thenThrow("当前[{}]账户不存在！",text);
        //密码是否正确
        boolean b = this.validPassword(password,po);
        Assert.succ(b).thenThrow("您提交的凭证有误，无法登录！");
        //
        return this.toResult(po);
    }

    public abstract PO selectPoByAccount(String username, int paltform);


    public abstract boolean validPassword(String password,PO po );

    public abstract RES toResult(PO po);

}
